home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 5 / GRAF3D / BOXSPHER.C < prev    next >
C/C++ Source or Header  |  1992-07-19  |  3KB  |  167 lines

  1. /*╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤ÑÑÑ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤*/
  2. /*                                                                               */
  3. /* BoxSphere.c    --    Translation of Pascal 'LISA/EXAMPLE/BOXSPHERE.TEXT'        */
  4. /*                    from Apple Programmer's and Developer's Association        */
  5. /*                    disk 'Macintosh Example Applications and Sources v.1.0'    */
  6. /*                                                                               */
  7. /*                    Translation to LightSpeed C by Lewis E. Garrett - 9/27/90  */
  8. /*                                                   CIS 71147,2202               */
  9. /*                                                                               */
  10. /*                                                                               */
  11. /*╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤ÑÑÑ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤*/
  12.  
  13. #include <Graf3D.h>
  14. #include <FixMath.h>
  15.  
  16. /* CONST */
  17. #define keyOrMouse mDownMask+keyDownMask
  18. #define NIL 0L
  19.  
  20. /*    GrafPort gPort; */
  21. Port3D         gPort3D;
  22. Fixed sinTable[31]; /*24 steps, 15 degree increments*/
  23. EventRecord dummy;
  24. Rect         inRect;
  25. WindowPtr     w;
  26. Port3D        *ptr3D;
  27.  
  28. main()
  29.  
  30. { /* main program */
  31.     FlushEvents(everyEvent, 0);
  32.     InitGraf(&thePort);
  33.     ptr3D    = &gPort3D;
  34.     InitGrf3d(&ptr3D);
  35.     InitFonts();
  36.     InitWindows();
  37.     InitMenus();
  38.     TEInit();
  39.     InitDialogs(NIL);
  40.  
  41.     InitCursor();
  42.     HideCursor();
  43.  
  44.     w = NewWindow(NIL, &screenBits.bounds, "\p", TRUE, 2, (WindowPtr)(-1L), FALSE, 0L);
  45.     SetPort(w);
  46.  
  47.     PenPat(white);
  48.     BackPat(black);
  49.  
  50.     EraseRect(&thePort->portRect);
  51.  
  52.     Draw3D();
  53.  
  54.     ShowCursor();
  55.     DisposeWindow(w);
  56. }
  57.  
  58.  
  59. Draw3D()
  60. {
  61.     Open3DPort(&gPort3D);
  62.     InitSinTable();
  63.  
  64.     FrameRect(&thePort->portRect);
  65.     inRect = thePort->portRect;
  66.     InsetRect(&inRect, 1, 1);
  67.     LookAt(-983040, 655360, 983040, -655360); /*LookAt (-15, 10, 15, -10)*/
  68.     ViewAngle(1966080); /*ViewAngle(30)*/
  69.     Roll(1310720); /*Roll(20)*/
  70.     Pitch(4915200); /*Pitch(75)*/
  71.  
  72.     do    {
  73.         EraseRect(&inRect);
  74.         Yaw(327680);            /*Yaw(5)*/
  75.         DrawCube(1048576);        /*DrawCube(16)*/
  76.         DrawSphere(524288);     /*DrawSphere(8)*/
  77.     }
  78.     while (!GetOSEvent(keyOrMouse, &dummy));
  79. }
  80.  
  81. InitSinTable()
  82. {
  83.     /* CONST */
  84. #define  f15DivRad 17157L
  85.     int iAng;
  86.  
  87.     for ( iAng = 0; iAng<=30; iAng++ )      {
  88.         sinTable[iAng] = FixDiv(FracSin(FixMul(FixRatio(iAng, 1),
  89.         f15DivRad)), 1073741824);
  90.  
  91.     }
  92. }
  93.  
  94. DrawCube(Edge)
  95. Fixed Edge;
  96. {
  97.     Fixed Half;
  98.  
  99.     Half = FixDiv (Edge, 131072);     /*Half := Edge / 2*/
  100.     Move3D(-Half,-Half,-Half);
  101.     Line3D(0,Edge,0);
  102.     Line3D(Edge,0,0);
  103.     Line3D(0,-Edge,0);
  104.     Line3D(-Edge,0,0);
  105.  
  106.     Move3D(0,0,Edge);
  107.     Line3D(0,Edge,0);
  108.     Line3D(Edge,0,0);
  109.     Line3D(0,-Edge,0);
  110.     Line3D(-Edge,0,0);
  111.  
  112.     Move3D(0,0,-Edge);
  113.     Line3D(0,0,Edge);
  114.     Move3D(0,Edge,-Edge);
  115.     Line3D(0,0,Edge);
  116.     Move3D(Edge,0,-Edge);
  117.     Line3D(0,0,Edge);
  118.     Move3D(0,-Edge,-Edge);
  119.     Line3D(0,0,Edge);
  120.  
  121.     Move3D(-Half,Half,-Half);
  122. }
  123.  
  124. Circle(radius)
  125. Fixed radius;
  126.  
  127. {
  128.     Fixed xCenter,
  129.     yCenter,
  130.     ang;
  131.     int iAng;
  132.  
  133.     xCenter = gPort3D.pen.x;
  134.     yCenter = gPort3D.pen.y;
  135.     MoveTo2D(xCenter, yCenter+radius);
  136.     for ( iAng = 0 ; iAng<=24; iAng++ )
  137.         LineTo2D(xCenter + FixMul(radius, sinTable[iAng]),
  138.         yCenter + FixMul(radius, sinTable[iAng + 6]));
  139.     MoveTo2D(xCenter, yCenter);
  140. }
  141.  
  142. DrawSphere(radius)
  143. Fixed radius;
  144.  
  145. {
  146.     int zPlane;
  147.     Fixed zStep,
  148.     dz;
  149.     Point3D center;
  150.  
  151.     /* WITH gPort3D */
  152.     center = gPort3D.pen;
  153.     zStep = FixDiv(radius, 524288);  /*zStep := radius / 8;*/
  154.     for ( zPlane = -7 ; zPlane<=7; zPlane++ )
  155.     {
  156.         dz = FixMul(zStep, FixRatio(zPlane, 1));
  157.         MoveTo3D(center.x, center.y, center.z + dz); /*Get on the right plane.*/
  158.         Circle(FracSqrt(FixMul(radius, radius) - FixMul(dz, dz)) >> 7);
  159.  
  160.         /*Draw a circle there.*/
  161.     }
  162.     MoveTo3D(center.x, center.y, center.z);
  163. }
  164.  
  165.  
  166.  
  167.